package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.Dish;
import com.itheima.bean.DishFlavor;
import com.itheima.dao.DishDao;
import com.itheima.dto.DishDto;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @date 2023/2/21
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品包含口味信息
     * @param dishDto
     */
    @Override
    public void addDishWithFlavors(DishDto dishDto) {
        //0.开启事务（需要在启动类上打上@EnableTransactionManagement 然后在方法上打上@Transactional）
        //1.保存菜品基本信息到dish表
        this.save(dishDto);  //insert into dish values ()

        //2.保存菜品口味信息到dish_flavor表中
        List<DishFlavor> flavors = dishDto.getFlavors();

        //2.1 关联菜品id
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }

//        int i = 1/0;
        //2.2 调用servie的批量保存方法
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 查询菜品信息包含口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getDishWithFlavors(Long id) {
        DishDto dishDto = new DishDto();
        //1.根据菜品id查询菜品表，得到菜品的基本信息封装到dishDto
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishDto);

        //2.根据菜品id查询菜品口味信息表，得到口味信息列表，封装到dishDto
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavors = dishFlavorService.list(wrapper);
        dishDto.setFlavors(flavors);

        //3.返回dishDto
        return dishDto;
    }

    /**
     * 修改菜品包含口味信息修改
     * @param dishDto
     */
    @Override
    public void updateWithFlavors(DishDto dishDto) {
        //0.开启事务（涉及多表更新操作）
        //1.调用父类的修改方法，修改菜品信息dish表
        this.updateById(dishDto);

        //2.修改口味信息（修改、删除、新增），简单一点直接先清空口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(wrapper); //delete from dish_flavor where dish_id = xxx  设置逻辑删除后 sql: update  xxx

        //3.调用dishFlavorService保存口味信息（关联菜品id）
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            //开启了逻辑删除后，需要设置id为空，否则出现id重复
            flavor.setId(null);
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 查询菜品列表包含口味信息
     * @param dish
     * @return
     */
    @Override
    public List<DishDto> listWithFlavors(Dish dish) {
        //1.创建查询对象
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        //2.设置查询条件：查询分类id、菜品名称name、菜品状态status
        wrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        wrapper.like(dish.getName() != null, Dish::getName, dish.getName());
        wrapper.eq(dish.getStatus()!=null, Dish::getStatus, dish.getStatus());

        //3.调用父类的查询方法，得到List<Dish>
        List<Dish> dishList = this.list(wrapper);

        //4.构造List<DishDto>数据
        List<DishDto> dishDtos = new ArrayList<>();

        //5.循环遍历List<Dish>，将dish所有属性值拷贝给DishDto
        for (Dish dish1 : dishList) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish1, dishDto);

            //6.查询口味信息列表，将其封装到dishDto中
            LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
            dishFlavorWrapper.eq(DishFlavor::getDishId, dish1.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(dishFlavorWrapper);
            dishDto.setFlavors(dishFlavors);

            //7.将dishDto添加到List<DishDto>
            dishDtos.add(dishDto);
        }

        //8.返回List<DishDto>给controller
        return dishDtos;
    }
}
